﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using com.gyz.hao.WinCapInfoApp.Model;
using yeqw.FrameWork;

namespace com.gyz.hao.WinCapInfoApp.BLL
{
    public class GroupBLL
    {
        public GroupInfo Group { set; get; }

        private List<CapInfo> capinfolist;
        private bool InitData()
        {
            capinfolist = AppConfigInfo.AppConfig.GetCapInfoListByGroupID(Group.ID);
            if (capinfolist == null || capinfolist.Count < 1)
            {
                AppConfigInfo.AppConfig.WriteMsg("未获取GroupID为:{0}的数据库数据".FormatExtension(Group.ID));
                return false;
            }
            return true;
        }
        public void Exec()
        {
            if (AppConfigInfo.AppConfig.GroupCollect == false)
            {
                if (!InitData()) 
                    return;
            }
            //这里要算出线程休眠时间
            int infoid = 0;
            int sleeptime = AppConfigInfo.AppConfig.sleeptime;
            if (sleeptime < 1000)
            {
                sleeptime = 1000;
            }

            while (!AppConfigInfo.AppConfig.IsStop)
            {
                MysqlDataBLL databll = new MysqlDataBLL();
                if(AppConfigInfo.AppConfig.GroupCollect == false)//如果是分组采集
                {
                    capinfolist = databll.GetCapInfoByGroupList(Group.ID);
                }
                else
                {
                    capinfolist = AppConfigInfo.AppConfig.CapInfoList;
                }
                foreach (CapInfo info in capinfolist)
                {
                    try
                    {
                        ExecCapInfoBLL bll = new ExecCapInfoBLL();
                        bll.capInfo = info;
                        infoid = info.id;
                        bll.Exec();//每一条数据的线程

                        //Thread thread = new Thread(bll.Exec, 1024 * 512);
                        //thread.IsBackground = true;
                        //thread.Name = "capthread_" + info.id;
                        //thread.Start();
                        //Thread.Sleep(sleeptime);

                    }

                    catch (Exception ex)
                    {
                        AppConfigInfo.AppConfig.WriteMsg("执行采集出现异常.异常原因：{0} infoid:{1}".FormatExtension(ex.Message, infoid));
                    }
                }
            }
        }
    }
}
